package cn.jwcat.utils.result;


import lombok.Data;
import org.springframework.stereotype.Component;

import java.io.Serializable;

@Data
@Component
@SuppressWarnings("all")
public class Result implements Serializable {

	private Integer code;

	private String message;

	private Object data;

	public static Result success() {
		Result result = new Result();
		result.setResultCodeAndMessage(ResultCodeEnum.SUCCESS);
		result.setData("数据操作成功");
		return result;
	}

	public static Result success(Object data) {
		Result result = new Result();
		result.setResultCodeAndMessage(ResultCodeEnum.SUCCESS);
		result.setData(data);
		return result;
	}

	public static Result error() {
		Result result = new Result();
		result.setResultCodeAndMessage(ResultCodeEnum.ERROR);
		result.setData("数据操作失败，请重新尝试。");
		return result;
	}

	public static Result error(Object data) {
		Result result = new Result();
		result.setResultCodeAndMessage(ResultCodeEnum.ERROR);
		result.setData(data);
		return result;
	}


	public static Result api(ResultCodeEnum resultCodeEnum) {
		Result result = new Result();
		result.setResultCodeAndMessage(resultCodeEnum);
		return result;
	}

	public static Result api(ResultCodeEnum resultCodeEnum, Object data) {
		Result result = new Result();
		result.setResultCodeAndMessage(resultCodeEnum);
		result.setData(data);
		return result;
	}

	public static Result code(Integer code) {
		Result result = new Result();
		result.setCode(code);
		return result;
	}


	public void setResultCodeAndMessage(ResultCodeEnum resultCodeEnum) {
		this.code = resultCodeEnum.getCode();
		this.message = resultCodeEnum.getMessage();
	}


	public Result message(String message) {
		this.message = message;
		return this;
	}


	public Result data(Object data) {
		this.data = data;
		return this;
	}
}
